﻿/* ==============================================================================
 * 功能描述：HUDBar  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/27 20:27:44
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using ZZQFrameWork;

public class HUDBar : MonoBehaviour
{
    [HideInInspector]
    public PlayerAI PlayerAI;

    private int lastHp;

    void Start()
    {
        lastHp = PlayerAI.Data.hp;
    }

    void OnEnable()
    {
        EventDispatcher.AddListener<int>(Events.RoleEvent.OnRoleLevelUp,UpgradeLv);
        EventDispatcher.AddListener<int, int>(Events.RoleEvent.OnUpdateHp, UpdateHp);
        EventDispatcher.AddListener<int,int>(Events.RoleEvent.OnUpdateExp,AddExp);
    }

    void OnDisable()
    {
        EventDispatcher.RemoveListener<int>(Events.RoleEvent.OnRoleLevelUp, UpgradeLv);
        EventDispatcher.RemoveListener<int, int>(Events.RoleEvent.OnUpdateHp, UpdateHp);
        EventDispatcher.RemoveListener<int, int>(Events.RoleEvent.OnUpdateExp, AddExp);
    }

    void UpgradeLv(int lv)
    {
        UIManager.Instance.ShowHUD("升级！等级："+lv,Color.yellow,1f);
    }

    void UpdateHp(int hp,int maxHp)
    {
        //Debug.Log("--"+lastHp+"==="+hp);
        if (hp > lastHp)
        {
            //加血
            UIManager.Instance.ShowHUD("+"+(hp-lastHp),Color.green,.5f);
        }
        else
        {
            //减血
            UIManager.Instance.ShowHUD("-" + ( lastHp-hp), Color.red, .5f);
        }
        lastHp = hp;
    }

    void AddExp(int exp,int maxExp)
    {
        //GameObject go = PoolManager.Instance.Spawn("Reward_number");
        //Text text = go.GetComponent<Text>();
        //text.text = "+" + exp;
        //UIManager.Instance.ShowHUD("+"+exp,Color.yellow,2f,UIManager.Instance.UIContainer.GetComponentInChildren<MainPanel>().transform.Find("PlayerState/Xp").position);
    }

}